﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleTerminal.Controls
{
    [ToolboxItem(true)]
    class ColorComboBox : BaseComboBox
    {
        protected Color selectedColor = Color.White;

        [Browsable(true)]
        [Category("自定义事件")]
        [Description("颜色改变时触发")]
        public event EventHandler SelectedColorChanged;



        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new string Text
        {
            get;
            set;
        }

        [Browsable(true)]
        [Description("当前选中的颜色")]
        [Category("自定义属性")]
        public Color SelectedColor
        {
            get
            {
                return selectedColor;
            }
            set
            {
                selectedColor = value;
                selectedIndex = Items.IndexOf(value);
                OnColorChanged(EventArgs.Empty);
            }
        }

        protected virtual void OnColorChanged(EventArgs eventArgs)
        {
            SelectedColorChanged?.Invoke(this, eventArgs);
        }

        protected override void OnSelectedIndexChanged(EventArgs eventArgs)
        {
            base.OnSelectedIndexChanged(eventArgs);
            SelectedColor = (Color)Items[SelectedIndex];

        }

        protected override void UpdateData()
        {
            base.UpdateData();
            Items.Clear();
            foreach (string name in Enum.GetNames(typeof(KnownColor)))
            {
                Items.Add(Color.FromName(name));
            }
        }

        protected override void OnDrawItem(DrawItemEventArgs eventArgs)
        {
            base.OnDrawItem(eventArgs);
            Color color = (Color)Items[eventArgs.Index];

            if (eventArgs.Hovered)
            {
                Pen pen = new Pen(Color.Gray, 1);
                eventArgs.Graphics.DrawRectangle(pen, eventArgs.Bounds.X + 2,
                   eventArgs.Bounds.Y + 2,
                   eventArgs.Bounds.Width - 5,
                   eventArgs.Bounds.Height - 5);
                pen.Dispose();
            }

            using (SolidBrush solidBrush = new SolidBrush(color))
            {
                eventArgs.Graphics.FillRectangle(solidBrush,
                    eventArgs.Bounds.X + 5,
                    eventArgs.Bounds.Y + 5,
                    eventArgs.Bounds.Width - 10,
                    eventArgs.Bounds.Height - 10);
            }
        }

        protected override void OnDrawContent(DrawComboBoxContentEventArgs eventArgs)
        {
            base.OnDrawContent(eventArgs);
            //  Pen pen = new Pen(Color.Gray, 1);
            using (SolidBrush brush = new SolidBrush(SelectedColor))
            using (GraphicsPath path = GetRoundRect(new Rectangle(eventArgs.Bounds.X,
                                   eventArgs.Bounds.Y,
                                   eventArgs.Bounds.Width - 1,
                                   eventArgs.Bounds.Height - 1), Radius))
            {

                eventArgs.Graphics.FillPath(brush, path);
                eventArgs.Graphics.DrawPath(SystemPens.ControlDark, path);
            }
        }
    }


}
